package com.runjian.rundo.iot.agent.common.network.tcp.handle;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * tcp 字节流消息处理器
 *
 * @author CaoQingHua
 */
@Slf4j
public abstract  class AbstraceTcpMessageHandler extends ChannelInboundHandlerAdapter implements MessageHandler {

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 配置日志流水号
        configTraceId(ctx);
        log.info("捕捉到异常：{}", cause);
        ctx.fireExceptionCaught(cause);
    }

    /**
     * 打印消息
     *
     * @param packet 缓存对象
     */
    public void printReceiveMessage(ByteBuf packet) {
        if (log.isDebugEnabled()) {
            log.debug("[接收消息] {}.", ByteBufUtil.hexDump(packet));
        }
    }
}
